package com.cans.web.base;

import com.alibaba.fastjson.JSONObject;
import com.cans.dto.ResDto;
import com.cans.model.pms.SysRole;
import com.cans.model.pms.SysUser;
import com.cans.service.pms.api.ISysRoleService;
import com.cans.service.pms.api.ISysUserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: cans
 * @date: 2018/4/5
 * @description:
 */
@Controller
@RequestMapping("/home")
public class HomeController {

    @Resource
    private ISysUserService userService;
    @Resource
    private ISysRoleService roleService;

    /**
     * 后台主布局
     * @return
     */
    @RequestMapping("/index")
    public String index(){

        return "home/index";
    }


    /**
     * 后台主页面
     * @return
     */
    @RequestMapping("/main")
    public String main(){

        return "home/main";
    }

    /**
     * 个人信息面板
     * @return
     */
    @GetMapping("/userInfo")
    public String userInfo(Model model){

        SysUser user = (SysUser) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();

        model.addAttribute("user",user);

        return "home/userInfo";
    }
    /**
     * 获取分类统计数据
     * @return
     */
    @RequestMapping(value = "/groupCount",method = RequestMethod.GET)
    @ResponseBody
    public ResDto groupCount()  {

        JSONObject jsonObject = new JSONObject();

        List<SysUser> userList = userService.findAll();
        List<SysRole> roleList = roleService.findAll();

        jsonObject.put("userCount",userList.size());
        jsonObject.put("roleCount",roleList.size());

        return ResDto.success(jsonObject);
    }
}
